layui.use(['table', 'layer'], function () {
    var layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        table = layui.table;
    //计划项数据展示
    var tableIns = table.render({
        elem: '#cusDevPlanList',
        url: ctx + '/cus_dev_plan/list?sid=' + $("input[name='sid']").val(),
        cellMinWidth: 95,
        page: true,
        height: "full-125",
        limits: [10, 15, 20, 25],
        limit: 10,
        toolbar: "#toolbarDemo",
        id: "cusDevPlanListTable",
        cols: [[
            {type: "checkbox", fixed: "center"},
            {field: "rowId", title: '编号', fixed: "true"},
            {field: 'planItem', title: '计划项', align: "center"},
            {field: 'exeAffect', title: '执行效果', align: "center"},
            {field: 'planDate', title: '执行时间', align: "center"},
            {field: 'createDate', title: '创建时间', align: "center"},
            {field: 'updateDate', title: '更新时间', align: "center"},
            {title: '操作', fixed: "right", align: "center", minWidth: 150, templet: "#cusDevPlanListBar"}
        ]]
    });


    table.on("toolbar(cusDevPlans)", function (obj) {
        switch (obj.event) {
            case "add" :
                openAddOrUpdateCusDevPlanDialog();
                break;
            case "success":
                updateSaleChanceDevResult($("input[name='sid']").val(), 2);
                break;
            case "failed":
                updateSaleChanceDevResult($("input[name='sid']").val(), 3);
                break;
        }
    });

    function updateSaleChanceDevResult(sid, devResult) {
        console.log(sid+devResult)
        layer.confirm("确认更新机会数据状态?", {icon: 3, title: "客户开发计划管理"}, function (index) {
            $.ajax(
                {
                    url:
                        ctx + "/sale_chance/updateSaleChanceDevResult",
                    data:
                        {rowId: sid, devResult: devResult},
                    type: "put",
                    success: function (data) {
                        if (data.code == 200) {
                            layer.msg("机会数据更新成功");
                            layer.closeAll("iframe");
                            // 刷新父页面
                            parent.location.reload();
                        } else {
                            layer.msg(data.msg);
                        }
                    }
                });
        });
        return false;
    }


    table.on("tool(cusDevPlans)", function (obj) {
        var layEvent = obj.event;
        if (layEvent === "edit") {
            openAddOrUpdateCusDevPlanDialog(obj.data.rowId);
        } else if (layEvent === "del") {
            layer.confirm("确认删除当前记录?", {icon: 3, title: "客户开发计划管理"}, function (index) {
                $.ajax({
                    url:
                        ctx + "/cus_dev_plan/delete",
                    data:
                        {rowId: obj.data.rowId},
                    type:
                        "delete",
                    success: function (data) {
                        if (data.code == 200) {
                            layer.msg("删除成功");
                            tableIns.reload();
                        } else {
                            layer.msg(data.msg);
                        }
                    }
                });
            });
        }
        return false;

    });


    function openAddOrUpdateCusDevPlanDialog(rowId) {
        var title = "计划项管理管理-添加计划项";
        var url = ctx + "/cus_dev_plan/addOrUpdateCusDevPlanPage?sid=" + $("input[name='sid']").val();
        if (rowId) {
            title = "计划项管理管理-更新计划项";
            url = url + "&rowId=" + rowId;
        }
        layui.layer.open({
            title: title,
            type: 2,
            area: ["700px", "500px"],
            maxmin: true,
            content: url
        });
    }


});
